package top.qitun.api.v1;

import java.security.Principal;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import top.qitun.bizz.client.client.entity.ClientBO;
import top.qitun.bizz.client.client.service.ClientService;

@RestController
@RequestMapping(path = "/v1")
public class UserControllerV1 {
	
	@Autowired
    private ClientService clientService;


    @RequestMapping(path = "/me")
    public ResponseEntity me(Principal principal) {
        ClientBO user = null;
        if(principal != null) {
            user = clientService.getUserByUsername(principal.getName());
        }

        return Optional.ofNullable(user)
                .map(a -> new ResponseEntity<ClientBO>(a, HttpStatus.OK))
                .orElseThrow(() -> new UsernameNotFoundException("Username not found"));
    }
}
